package com.example.demoexample.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.demoexample.R;

public class SlideView extends View {
    private static final int PAY_FTEE = 0; //免结算
    private static final int PAY_CARD = 1; //权益卡
    private static final int PAY_LIYU = 2; //礼遇

    private int DEFAULT_HEIGHT;
    private int mViewHeight;            // 整个View的高度
    private int mViewWidth;      // 整个View的宽度
    private Paint rectPaint;
    private Paint textPaint;
    private Paint slidePaint;

    private int beginX;  //滑块X坐标
    private int beginY;  //滑块Y坐标

    private int originX; //滑块起始位置

    private int startX;

    private int lastX;
    private int endX;   //滑块最远的点
    private int slideWeith;  //滑块宽度
    private int slideHight; //滑块高度

    private boolean isChange = true;

    public SlideView(Context context) {
        this(context, null);
    }

    public SlideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, -1);
    }

    public SlideView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        initPaint();
        DEFAULT_HEIGHT = dpToPx(context, 120);
        beginX = dpToPx(context, 20);
        beginY = dpToPx(context, 25);
        slideWeith = dpToPx(context, 80);
        slideHight = dpToPx(context, 70);
        originX = beginX;

        startX = dpToPx(context, 25);
    }

    private void initPaint() {
        rectPaint = new Paint();
        rectPaint.setColor(getResources().getColor(R.color.color_8BC349));
        rectPaint.setStyle(Paint.Style.FILL);

        textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setStrokeWidth(8);
        textPaint.setTextSize(50);
        textPaint.setTextAlign(Paint.Align.CENTER);

        slidePaint = new Paint();
        slidePaint.setColor(getResources().getColor(R.color.color_239B25));
        slidePaint.setStyle(Paint.Style.FILL);
    }

    public void setType(int type) {
        switch (type) {
            case PAY_FTEE:

                break;
            case PAY_CARD:

                break;
            case PAY_LIYU:

                break;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidthSize = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeightSize = MeasureSpec.getSize(heightMeasureSpec);
        int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (measureWidthMode == MeasureSpec.AT_MOST
                && measureHeightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthMeasureSpec, DEFAULT_HEIGHT);
        } else if (measureWidthMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthMeasureSpec, measureHeightSize);
        } else if (measureHeightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(measureWidthSize, DEFAULT_HEIGHT);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewHeight = h;
        mViewWidth = w;

        endX = mViewWidth - slideWeith - beginX;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        RectF rectF = new RectF(0, 0, mViewWidth, mViewHeight);
        canvas.drawRoundRect(rectF, 10, 10, rectPaint);

        String text = "滑动光标完成取车>>>";
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        float baseline = rectF.centerY() + distance;

        RectF rectSlide = new RectF(beginX, beginY, beginX + slideWeith, beginY + slideHight);
//        RectF rectSlide = new RectF(endX, beginY, endX + slideWeith, beginY + slideHight);
//        canvas.drawRoundRect(rectSlide, 0, 0, slidePaint);

        Path path = new Path();
        path.moveTo(beginX, beginY);
        path.lineTo(beginX + 2 * startX, beginY);
        path.lineTo(beginX + slideWeith, beginY + slideHight / 2);
        path.lineTo(beginX + 2 * startX, beginY + slideHight);
        path.lineTo(beginX, beginY + slideHight);
        canvas.drawPath(path, slidePaint);

        canvas.drawText(text, rectF.centerX(), baseline, textPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        //获取手机触摸的坐标
        int x = (int) event.getX();

        switch (action) {
            case MotionEvent.ACTION_DOWN://按下,获取初始的位置
                lastX = x;
                if (lastX > beginX + slideWeith || lastX < beginX) {
                    isChange = false;
                } else {
                    isChange = true;
                }
                Log.e("SlideView", "ACTION_DOWN isChange = " + isChange);
                break;
            case MotionEvent.ACTION_MOVE://移动,小球跟随手指的移动
                Log.e("SlideView", "ACTION_MOVE isChange = " + isChange);
                if (!isChange) {
                    break;
                }

                int offsetX = x - lastX;
                int x2 = (int) event.getX();

                if (x2 > originX) {
                    if (endX - x2 > 0) {
                        beginX = x2;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP://当手指抬起时,回到小球初始的位置
                Log.e("SlideView", "ACTION_UP isChange = " + isChange);
                if (!isChange) {
                    break;
                }
                int x1 = (int) event.getX();
                if (endX - x1 > 0) {
                    beginX = originX;
                } else {
                    beginX = endX;
                }
                invalidate();
                break;
        }
        return true;
    }

    private int dpToPx(Context context, int dp) {
        return (int) (dp * context.getResources().getDisplayMetrics().density);
    }


}
